package com.someclip.component.container 
{
	import com.someclip.component.interfaces.IBox;
	import com.someclip.component.interfaces.IComponent;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Argus
	 */
	public class Box extends Container implements IBox 
	{
		private var _direction:String=Direction.VERTICAL;
		private var _hGap:Number=1;
		private var _vGap:Number=1;
		public function Box() 
		{
			this.fillColor = -1;
			
		}
		
		override public function updateLayout():void
		{
			super.updateLayout();
			updateChilds();
		}
		
		private function updateChilds():void 
		{
			var len:uint = childList.length;
			if (len == 0)
				return;
			var lastCrood:Number = 0;
			var gap:Number;
			var crood:String;
			var referProperty:String;
			var referWH:String;
			if (_direction == Direction.VERTICAL)
			{
				gap = _vGap;
				crood = "y";
				referProperty = "top";
				referWH = "height";
			}else
			{
				gap = _hGap;
				crood = "x";
				referProperty = "left";
				referWH = "width";
			}
			for (var i:uint = 0; i < len; i++ )
			{
				var child:Object = childList[i];
				var bound:Rectangle = child.bound;
				child.children[crood] = lastCrood + gap - bound[referProperty];
				lastCrood = lastCrood + gap + bound[referWH];
			}
			crood = null;
			referProperty = null;
			referWH = null;
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			if (child is IComponent)
			{
				(child as IComponent).top = NaN;
				(child as IComponent).bottom = NaN;
				(child as IComponent).vCenter = NaN;
				(child as IComponent).left = NaN;
				(child as IComponent).right = NaN;
				(child as IComponent).hCenter = NaN;
			}
			super.addChild(child);
			updateChilds();
			return child;
		}
		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			if (child is IComponent)
			{
				(child as IComponent).top = NaN;
				(child as IComponent).bottom = NaN;
				(child as IComponent).vCenter = NaN;
				(child as IComponent).left = NaN;
				(child as IComponent).right = NaN;
				(child as IComponent).hCenter = NaN;
			}
			super.addChildAt(child, index);
			updateChilds();
			return child;
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			super.removeChild(child);
			updateChilds();
			return child;
		}
		
		override public function removeChildAt(index:int):DisplayObject
		{
			var child:DisplayObject = super.removeChildAt(index);
			updateChilds();
			return child;
		}
		
		/* INTERFACE com.someclip.component.interfaces.ISomeBox */
		
		public function get direction():String 
		{
			return _direction;
		}
		
		public function set direction(value:String):void 
		{
			_direction = value;
		}
		
		public function get hGap():Number 
		{
			return _hGap;
		}
		
		public function set hGap(value:Number):void 
		{
			_hGap = value;
		}
		
		public function get vGap():Number 
		{
			return _vGap;
		}
		
		public function set vGap(value:Number):void 
		{
			_vGap = value;
		}
		
	}

}